package com.zrwl.poscloud.core.restapi.admin.baseInfo;


import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.base.enums.EBusiType;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.DeptUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.EmployeeAccountRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 账户业务 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-10-20
 */
@RestController
@Api(value = "账户业务相关接口", tags = {"账户业务相关接口"})
@RequestMapping("/employeeAccountRecord")
@Slf4j
public class EmployeeAccountRecordRestApi {
    @Autowired
    private EmployeeAccountRecordService employeeAccountRecordService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private EmployeeService employeeService;
    @AuthorityVerify
    @ApiOperation(value = "获取账户业务列表", notes = "获取账户业务列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody EmployeeAccountRecordVO employeeAccountRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取账户业务列表: {}", employeeAccountRecordVO);
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeAccountRecordVO.setCompanyUid(adminVO.getCompanyUid());
        if (employeeAccountRecordVO.getCompanyCode() == null) {
            employeeAccountRecordVO.setCompanyCode(DeptUtil.getLatestCompnay(adminVO.getCompanyCode()));
        } else {
            employeeAccountRecordVO.setCompanyCode(DeptUtil.getLatestCompnay(employeeAccountRecordVO.getCompanyCode()));
        }
        return ResultUtil.successWithData(employeeAccountRecordService.getPageList(employeeAccountRecordVO));
    }

    @AuthorityVerify
    @ApiOperation(value = "获取账户业务列表", notes = "获取账户业务列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody EmployeeAccountRecordVO employeeAccountRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取账户业务列表: {}", employeeAccountRecordVO);
        return ResultUtil.successWithData(employeeAccountRecordService.getAllList(employeeAccountRecordVO));
    }

    @AuthorityVerify
    @OperationLogger(value = "新增账户业务")
    @ApiOperation(value = "新增账户业务", notes = "新增账户业务", response = String.class)
    @PostMapping("/add")
    @Transactional
    public String add(@Validated({Insert.class}) @RequestBody EmployeeAccountRecordVO employeeAccountRecordVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("新增账户业务: {}", employeeAccountRecordVO);
        employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountOpen);//新增
        employeeAccountRecordVO.setUid(null);
        employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());
        employeeAccountRecordService.createAccount(employeeAccountRecordVO, adminService.getMe());
        return ResultUtil.successWithData(employeeAccountRecordVO);
    }

    @SneakyThrows
    @AuthorityVerify
    @OperationLogger(value = "修改账户业务")
    @ApiOperation(value = "修改账户业务", notes = "修改账户业务", response = String.class)
    @PostMapping("/edit")
    @Transactional
    public String edit(@Validated({Update.class}) @RequestBody EmployeeAccountRecordVO employeeAccountRecordVO, BindingResult result) {
        if (StringUtils.isEmpty(employeeAccountRecordVO.getEmployeeUid()) || StringUtils.isEmpty(employeeAccountRecordVO.getEmployeeUid().trim())) {
            ThrowableUtils.checkParamArgument(result);
        }
        log.info("修改账户业务: {}", employeeAccountRecordVO);
        /***
         * service 抛出RuntimeException
         * 1.create账户业务记录
         * 2.修改employeeInfo，将来会拓展人脸开关的相关接口，本质上是创建一张人脸卡信息
         * 3.创建icCardLog
         * 4.创建adminLog
         *
         */
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeAccountRecordVO.setUid(null);
        employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountEdit);
        employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());
        employeeAccountRecordService.editAccount(employeeAccountRecordVO, adminVO);
        return ResultUtil.successWithMessage("办理成功");
    }

    @AuthorityVerify
    @OperationLogger(value = "账户离开业务")
    @ApiOperation(value = "账户离开业务", notes = "账户离开业务", response = String.class)
    @PostMapping("/leave")
    @Transactional
    public String leave(@Validated({Update.class}) @RequestBody EmployeeAccountRecordVO employeeAccountRecordVO, BindingResult result) {
        if (StringUtils.isEmpty(employeeAccountRecordVO.getEmployeeUid()) || StringUtils.isEmpty(employeeAccountRecordVO.getEmployeeUid().trim())) {
            ThrowableUtils.checkParamArgument(result);
        }
        log.info("账户离开业务: {}", employeeAccountRecordVO);
        /***
         * service 抛出RuntimeException
         * 1.create账户业务记录
         * 2.修改employeeInfo 状态为2-离开
         * 3.取得所有使用中的卡
         * 4.将所有卡片的授权状态修改为无效 t_ic_card_auth_list
         * 5.所有卡创建icCardLog 具体参考挂失卡业务流程，与之业务一致
         * 6.将所有使用中的卡状态修改为挂失中
         * 7.将所有挂失中的卡修改为停用状态 一条sql直接update
         * 创建adminLog
         */
        AdminVO adminVO = adminService.getMe();
        if (!adminVO.isCompany()) {
            return ResultUtil.errorWithMessage("仅商户用户支持此功能");
        }
        employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountLeave);
        employeeAccountRecordVO.setUid(null);
        employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());
        employeeAccountRecordService.leaveAccount(employeeAccountRecordVO,adminVO);
        return ResultUtil.successWithMessage("办理成功");
    }

    @AuthorityVerify
    @OperationLogger(value = "获取自动生成持卡人编号")
    @ApiOperation(value = "获取自动生成持卡人编号", notes = "获取自动生成持卡人编号", response = String.class)
    @PostMapping("/getAutoEmployeeCode")
    public String getAutoEmployeeCode() {
        log.info("获取自动生成持卡人编号" + new Date());
        return ResultUtil.successWithData(employeeService.getAutoEmployeeCode());
    }
}
